package view.frontend;

import exceptionManager.ExceptionManager;
import view.generaldesign.DesignLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.*;
import model.User;
import utility.Security;
import model.handlers.UserHandler;
import view.ViewHandler;

/**
 * 
 * @author Sonny Sandberg
 */
public class Login
{
    private JPanel loginPanel;
    private JTextField tfieldUsername;
    private JPasswordField pfieldPassword;
    private JLabel lblErrorMessage;

    public Login()
    {
        ActionListener actionListener =
        (
            new ActionListener() 
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    doLogin();
                }
            }
        );
        
        loginPanel = new JPanel(null);
        loginPanel.setOpaque(false);
        loginPanel.setBounds(330, 350, 500, 400);

        JLabel lblHeadline = new JLabel("LOGIN");
        lblHeadline.setBounds(0, 20, 500, 50);
        lblHeadline.setHorizontalAlignment(SwingConstants.CENTER);
        lblHeadline.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 24f));

        JLabel lblUsername = new JLabel("Brugernavn");
        lblUsername.setBounds(20, 80, 150, 50);
        lblUsername.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 20f));

        JLabel lblPassword = new JLabel("Adgangskode");
        lblPassword.setBounds(20, 135, 150, 50);
        lblPassword.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 20f));

        JButton btnLogin = new JButton("login");
        btnLogin.setBounds(335, 135, 90, 50);
        btnLogin.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 15f));
        btnLogin.setBorder(DesignLayout.T_FIELD_RED);
        btnLogin.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        
        btnLogin.addActionListener(actionListener);

        tfieldUsername = new JTextField();
        tfieldUsername.setBounds(180, 80, 150, 50);
        tfieldUsername.setBorder(DesignLayout.T_FIELD_RED);  
        tfieldUsername.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tfieldUsername.addActionListener(actionListener);
        
        tfieldUsername.setHorizontalAlignment(SwingConstants.CENTER);
        tfieldUsername.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 15f));

        pfieldPassword = new JPasswordField();
        pfieldPassword.setBounds(180, 135, 150, 50);
        pfieldPassword.setBorder(DesignLayout.T_FIELD_RED);
        pfieldPassword.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        pfieldPassword.setHorizontalAlignment(SwingConstants.CENTER);
        pfieldPassword.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 15f));
        pfieldPassword.addActionListener(actionListener);
        
        lblErrorMessage = new JLabel("", JLabel.RIGHT);
        lblErrorMessage.setBounds(20, 195, 405, 25);
        lblErrorMessage.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD, 13f));
        lblErrorMessage.setForeground(DesignLayout.T_FIELD_BORDER_RED);
        
        loginPanel.add(lblHeadline);
        loginPanel.add(lblUsername);
        loginPanel.add(lblPassword);
        loginPanel.add(lblErrorMessage);
        loginPanel.add(btnLogin);
        loginPanel.add(tfieldUsername);
        loginPanel.add(pfieldPassword);
        
        loginPanel.setVisible(false);
    }
    
    public void doLogin()
    {
        try
        {
            boolean loggedIn = UserHandler.login(tfieldUsername.getText(), Security.encryptString(String.valueOf(pfieldPassword.getPassword())));
            if (loggedIn == true)
            {
                lblErrorMessage.setText("");                                    // Hvis der tidligere har været en fejlmeddelese, så fjerner vi den nu
                // Hide Login Panel
                ViewHandler.toggleAllsView("login", false);
                
                // Top Menu
                view.ViewSingleton.getSingletonObject().getMenus()[0].createTopMenu();
                ViewHandler.toggleAllsView("menuTop", true);
                
                // Left Menu
                view.ViewSingleton.getSingletonObject().getMenus()[1].createLeftMenu();
                
                // Set name of who is logged in on label
                JLabel userLabel = (JLabel) ViewHandler.getAllViewsPanel("userLoggedInPanel").getComponent(0);
                userLabel.setText("Logged in as: "+model.BSSingleton.getSingletonObject().getLoggedInUser().getUsername());
                ViewHandler.toggleAllsView("userLoggedInPanel", true);
                
                ViewHandler.toggleAllsView("orderContainer", true);
            }
            else
            {
                User user = new User();
                user.setUsername(tfieldUsername.getText());
                boolean isUsernameInDatabase = UserHandler.isUsernameAvailable(user);
                // Metoden der bruges, giver true, hvis brugernavnet ikke er i databasen
                if (isUsernameInDatabase == true)
                {
                    // Hvis brugernavnet ikke er i databasen, meddel brugeren dette
                    lblErrorMessage.setText("Brugernavnet er ikke registreret");
                }
                else
                {
                    // Hvis brugernavnet er i databasen, så er adgangskode forkert, meddel dette
                    lblErrorMessage.setText("Adgangskoden er forkert");
                }
            }
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
    }

    public JPanel getPanel()
    {
        return loginPanel;
    }
}